<?php

class User extends Zend_Db_Table
{
    protected $_name = 'user';
    
    public function getUser($id)
    {
        $user = $this->fetchRow('id='.$id);
        return $user['last_name'].' '.$user['first_name'];
    }
    public function getUserByMail($email)
    {
        $user = $this->fetchRow('email="'.$email.'"');
        return $user->id > 0 ? $user->id : false;
    }
    
    public function getUserInfo($id)
    {
        $select = $this->getAdapter();
        $prepare = $select->query('
                SELECT user.*
                FROM user
                WHERE user.id='.$id.'
                ');
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result[0];
    }
    
    public function getTeamUsers($user_id)
    {
        $select = $this->getAdapter();
        $prepare = $select->query('
                SELECT user.id,user.first_name,user.last_name,user.sex,department.name
                FROM user
                INNER JOIN start_page on start_page.user_id=user.id
                LEFT JOIN department on department.id=user.group_id
                WHERE start_page.team_id='.(int)$user_id.'
                AND deleted!=1
                ORDER BY user.last_name');
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result;
    }
    
    public function getGroupUsers()
    {
        $select = $this->getAdapter();
        $prepare = $select->query('
                SELECT user.id,user.first_name,user.last_name, user.group_id, department.name
                FROM user
                LEFT JOIN department on department.id=user.group_id
                WHERE deleted!=1
                ORDER BY user.group_id DESC,user.last_name');
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result;
    }
    
    public function ClearUserGroup($group_id)
    {
        $select = $this->getAdapter();
        $prepare = $select->query('UPDATE user SET group_id=0 
                                   WHERE deleted!=1 AND group_id='.$group_id);
    }
    
    public function SetUserGroup($group_id,$user_id)
    {
        $data = array(
            'group_id' => $group_id,
        );
        $where = 'id = ' . $user_id;
        $this->update($data, $where);
    }
    
    function validate($id,$params,$validate_unique_email=true)
    {
        $valid = true;
        $message = '';
        if(strlen($params['first_name'])==0)
        {
            $message .= 'Please, enter Firstname.<br>';
            $valid = false;
        }    
        if(strlen($params['last_name'])==0)
        {
            $message .= 'Please, enter Lastname.<br>';
            $valid = false;
        }  
        if(array_key_exists('username',$params)){  
        if(strlen($params['username'])==0)
        {
            $message .= 'Please, enter Username.<br>';
            $valid = false;
        }    
        if(strlen($params['password'])==0)
        {
            $message .= 'Please, enter Password.<br>';
            $valid = false;
        }    
        
        if($valid && $params['username'])
        {
            if($this->fetchRow('id <> '.$id.' and username="'.$params['username'].'"'))
            {
                $message .= 'User : Username already exist.<br>';
                $valid = false;
            }
        }        
        }
        
        if(array_key_exists('email',$params)){  
            //email
            $email_pattern = '/^[A-Z0-9\._\-]+@[A-Z0-9\.\-]+\.[A-Z0-9]{2,6}$/i';
            
            if(!preg_match($email_pattern,$params['email']))
            {
                $message .= 'Invalid Email.<br>';
                $valid = false;
            } 
            if($valid && $validate_unique_email )
            {
                if($this->fetchRow('id <> '.$id.' and email="'.$params['email'].'"'))
                {
                    $message .= 'Email already exist.<br>';
                    $valid = false;
                }
            }
        }
        
        if(array_key_exists('birthday',$params) && $params['birthday']) {
        //date 
        $date_pattern = '/^[0-9]{4}\-(?:0[1-9]|1[0-2])\-(?:[0-2]?[0-9]|3[0-1])$/';
        if(!preg_match($date_pattern,$params['birthday']))
        {
            $message .= 'Invalid birthday '.$params['birthday'].'. Format yyyy-mm-dd.<br>';
            $valid = false;
        } 
        }
        return $valid?$valid:$message;
    }
    
}
